import Vue from "vue";
import VueRouter from "vue-router";
import store from "@store";

// notes push
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
  { path: "/login", name: "login", component: () => import("@views/Login") },
  {
    path: "/",
    name: "home",
    component: () => import("@views/Home"),
    isIndex: true,
    redirect: { name: "bookCenter" },
    children: [
      // {
      //   path: '/index',
      //   name: 'index',
      //   component: () => import('@views/home/Index'),
      //   meta: {
      //     title: '首页',
      //     icon: '',
      //     show: true
      //   }
      // },
      {
        path: "/bookCenter",
        name: "bookCenter",
        component: () => import("@views/home/BookCenter"),
        meta: {
          title: "图书中心",
          icon: "el-icon-notebook-1",
          show: true,
        },
      },
      {
        path: "/hotBook",
        name: "hotBook",
        component: () => import("@views/home/HotBook"),
        meta: {
          title: "图书推荐",
          icon: "",
          show: false,
        },
      },
      {
        path: "/bookCenter/:id",
        name: "bookDetail",
        component: () => import("@views/home/BookCenter/BookDetail"),
        props: true,
      },
      {
        path: "/bookCenter/:id/:cid",
        name: "bookChapter",
        component: () => import("@views/home/BookCenter/BookChapter"),
        props: true,
      },
      // {
      //   path: "/discuss",
      //   name: "discuss",
      //   component: () => import("@views/home/Discuss"),
      //   meta: {
      //     title: "图书论坛",
      //     icon: "",
      //     show: true,
      //   },
      // },
      // {
      //   path: "/add-post",
      //   name: "addPost",
      //   component: () => import("@views/home/Discuss/AddPost"),
      // },
      // {
      //   path: "/discuss/:id",
      //   name: "discussDetail",
      //   component: () => import("@views/home/Discuss/PostDetail"),
      //   props: true,
      // },
      {
        path: "/errata",
        name: "errata",
        component: () => import("@views/home/Errata"),
        meta: {
          title: "图书报修",
          icon: "el-icon-setting",
          show: true,
        },
      },
      // {
      //   path: "/messageBoard",
      //   name: "messageBoard",
      //   component: () => import("@views/home/MessageBoard"),
      // },
      // {
      //   path: "/cart",
      //   name: "cart",
      //   component: () => import("@/views/home/Cart"),
      //   meta: {
      //     title: "购物车",
      //     icon: "el-icon-shopping-cart-full",
      //     show: true,
      //   },
      // },
      {
        path: "/info",
        name: "info",
        component: () => import("@views/home/Info"),
        meta: {
          title: "个人中心",
          icon: "el-icon-user",
          show: true,
        },
      },
      {
        path: "/ipublic",
        name: "ipublic",
        component: () => import("@views/home/iPublic"),
        meta: {
          title: "查看公告",
          icon: "el-icon-warning-outline",
          show: true,
        },
      },
    ],
  },
  {
    path: "/admin",
    name: "admin",
    isAdmin: true,
    component: () => import("@views/Admin"),
    redirect: { name: "user" },
    children: [
      {
        path: "/count",
        name: "count",
        component: () => import("@views/admin/count"),
        meta: {
          title: "统计",
          icon: "el-icon-user",
          show: true,
        },
      },
      {
        path: "/user",
        name: "user",
        component: () => import("@views/admin/User"),
        meta: { title: "用户管理", show: true, icon: "" },
      },
      // {
      //   path: "/BookManager",
      //   name: "BookManager",
      //   component: () => import("@views/admin/BookManager"),
      //   meta: { title: "书籍管理", show: true, icon: "" },
      //   children: [
      //     {
      //       path: "/BookManager/category",
      //       name: "category",
      //       component: () => import("@views/admin/Category"),
      //       meta: { title: "分类管理", show: true, icon: "" },
      //     },
      //     {
      //       path: "/BookManager/book",
      //       name: "book",
      //       component: () => import("@views/admin/Book"),
      //       meta: { title: "书籍管理", show: true, icon: "" },
      //     },
      //   ],
      // },
      {
        path: "/BookManager/category",
        name: "category",
        component: () => import("@views/admin/Category"),
        meta: { title: "分类管理", show: true, icon: "" },
      },
      {
        path: "/BookManager/book",
        name: "book",
        component: () => import("@views/admin/Book"),
        meta: { title: "书籍管理", show: true, icon: "" },
      },
      // {
      //   path: "/order",
      //   name: "order",
      //   component: () => import("@views/admin/Order"),
      //   meta: { title: "订单管理", show: true, icon: "" },
      // },
      // {
      //   path: "/DiscussManager",
      //   name: "DiscussManager",
      //   component: () => import("@views/admin/DiscussManager"),
      //   meta: { title: "论坛管理", show: true, icon: "" },
      //   children: [
      //     {
      //       path: "/DiscussManager/post",
      //       name: "post",
      //       component: () => import("@views/admin/Post"),
      //       meta: { title: "帖子管理", show: true, icon: "" },
      //     },
      //     {
      //       path: "/DiscussManager/comment",
      //       name: "comment",
      //       component: () => import("@views/admin/Comment"),
      //       meta: { title: "评论管理", show: true, icon: "" },
      //     },
      //   ],
      // },
      {
        path: "/book/:id",
        name: "chapter-list",
        component: () => import("@views/admin/Book/ChapterList"),
        props: true,
      },
      // {
      //   path: "/message",
      //   name: "message",
      //   component: () => import("@views/admin/Message"),
      //   meta: { title: "留言管理", show: false, icon: "" },
      // },
      {
        path: "/correction",
        name: "correction",
        component: () => import("@views/admin/Correction"),
        meta: { title: "报修处理", show: true, icon: "" },
      },
      {
        path: "/borrow",
        name: "borrow",
        component: () => import("@views/admin/Borrow"),
        meta: { title: "借取审核", show: true, icon: "" },
      },
      {
        path: "/admininfo",
        name: "admininfo",
        component: () => import("@views/admin/adminInfo"),
        meta: {
          title: "个人中心",
          icon: "el-icon-user",
          show: true,
        },
      },
      {
        path: "/point",
        name: "point",
        component: () => import("@views/admin/point"),
        meta: {
          title: "积分管理",
          icon: "el-icon-user",
          show: true,
        },
      },
      {
        path: "/public",
        name: "public",
        component: () => import("@views/admin/public"),
        meta: {
          title: "公告管理",
          icon: "el-icon-user",
          show: true,
        },
      },
      // {
      //   path: "/log",
      //   name: "log",
      //   component: () => import("@views/admin/Log"),
      //   meta: { title: "日志管理", show: true, icon: "" },
      // },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  const path = to.path.split("/")[1];
  const filterRoute = routes.find((i) => i.name === "admin");
  const adminRouter = filterRoute.children.map((i) => i.name);
  const user = store.state.currentUser;
  if (user === false || user === null || user === "") {
    if (path === "login") {
      next();
    } else {
      next({ name: "login" });
    }
  } else {
    if (path === "login") {
      next(from.path);
      // ques next(false) invalid
    } else {
      if (user.level !== 1 && adminRouter.includes(to.name)) {
        next("/");
      } else {
        next();
      }
    }
  }
  next();
});
export default router;
